<template>
  <div id="app">
    <!-- 主内容区域 -->
    <div class="main-content" :class="{ 'detail-page': isDetailPage }">
      <router-view />
    </div>
    
    <!-- 底部Tab导航 -->
    <van-tabbar v-model="activeTab" @change="onTabChange" v-show="!isDetailPage">
      <van-tabbar-item icon="home-o" to="/">首页</van-tabbar-item>
      <van-tabbar-item icon="credit-pay" to="/wallet">卡包</van-tabbar-item>
      <van-tabbar-item icon="orders-o" to="/orders">订单</van-tabbar-item>
      <van-tabbar-item icon="user-o" to="/profile">我的</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()

// 当前激活的Tab
const activeTab = ref(0)

// Tab配置
const tabConfig = [
  { path: '/', index: 0 },
  { path: '/wallet', index: 1 },
  { path: '/orders', index: 2 },
  { path: '/profile', index: 3 }
]

// 详情页面路径（不显示底部Tab）
const detailPages = ['/product/', '/groupbuy/', '/payment/']

// 判断是否为详情页面
const isDetailPage = computed(() => {
  return detailPages.some(path => route.path.startsWith(path))
})

// 根据路由设置激活的Tab
const setActiveTab = () => {
  const currentPath = route.path
  const tab = tabConfig.find(tab => tab.path === currentPath)
  if (tab) {
    activeTab.value = tab.index
  }
}

// Tab切换处理
const onTabChange = (index: number) => {
  const tab = tabConfig.find(tab => tab.index === index)
  if (tab && tab.path !== route.path) {
    router.push(tab.path)
  }
}

// 监听路由变化
watch(() => route.path, setActiveTab, { immediate: true })
</script>

<style>
#app {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.main-content {
  flex: 1;
  overflow-y: auto;
  padding-bottom: 50px; /* 为底部Tab留出空间 */
}

/* 详情页面不显示底部Tab */
.main-content.detail-page {
  padding-bottom: 0;
}
</style>
